package com.xoa.service.party;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.party.FixFormMapper;
import com.xoa.dao.party.GridEventMapper;
import com.xoa.dao.party.GridInfoMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.workflow.FlowRunPrcsMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.department.Department;
import com.xoa.model.party.*;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowRunPrcs;
import com.xoa.util.*;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.common.wrapper.BaseWrappers;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by gsb on 2017/9/6.
 */
@Service
public class GridEventService {
    public static final Integer PRCS_FLAG_WILL=1;
    public static final Integer PRCS_FLAG_OVER=2;
    @Autowired
    private GridEventMapper gridEventMapper;
    @Autowired
     private GridInfoMapper gridInfoMapper;
    @Autowired
    private SysCodeMapper sysCodeMapper;
    @Autowired
    private FlowRunPrcsMapper flowRunPrcsMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private FixFormMapper fixFormMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    /**
     * 添加返回tablleName和tableId
     * @param gridEvent
     * @return
     */
    public BaseWrapper addGridEvent(GridEvent gridEvent, String dateTime){
        BaseWrapper baseWrappers=new BaseWrapper();
        baseWrappers.setStatus(true);
        if(StringUtils.checkNull(gridEvent.getEventType())){
            baseWrappers.setFlag(false);
            baseWrappers.setMsg("err");
            return  baseWrappers;
        }
        gridEvent.setEventTime(DateFormat.getDate(dateTime));
        int a= gridEventMapper.insertSelective(gridEvent);
        if(a>0){
            gridEvent.setInfoName(gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId()).getGridName());
            gridEvent.setTableName("grid_event");
            baseWrappers.setFlag(true);
            baseWrappers.setMsg("ok");
            baseWrappers.setData(gridEvent);
            return  baseWrappers;

        }else {

            baseWrappers.setFlag(false);
            baseWrappers.setMsg("err");
            return  baseWrappers;
        }

    }
    public BaseWrapper selectByPrimaryKey(Integer eventId, HttpServletRequest request){
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        BaseWrapper baseWrappers=new BaseWrapper();
        baseWrappers.setStatus(true);
       GridEvent gridEvent=gridEventMapper.selectByPrimaryKey(eventId);
       if(gridEvent!=null){
           gridEvent.setUserName(usersMapper.getUsernameByUserId(gridEvent.getUserId()));
           gridEvent.setInfoName(gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId()).getGridName());
           gridEvent.setDateTime(DateFormat.getStrDate(gridEvent.getEventTime()));
           gridEvent.setCodeName(sysCodeMapper.getSingleCode(gridEvent.getEventTypeParent(),gridEvent.getEventType()).getCodeName());
           gridEvent.setGridName(gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId()).getGridName());
           if (gridEvent.getAttachmentName() != null && gridEvent.getAttachmentId() != null) {
               gridEvent.setAttachments2(GetAttachmentListUtil.returnAttachment(gridEvent.getAttachmentName(), gridEvent.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_grid));
           }
           FlowRunPrcs flowRunPrcs=flowRunPrcsMapper.queryByRunId(gridEvent.getRumId());
           if (flowRunPrcs != null) {
               gridEvent.setPrcsId(flowRunPrcs.getPrcsId().toString());
           } else {
               gridEvent.setPrcsId("");
           }
       }
        baseWrappers.setFlag(true);
        baseWrappers.setMsg("ok");
        baseWrappers.setData(gridEvent);
        return  baseWrappers;
    }
    public BaseWrapper deleteByPrimaryKey(Integer eventId){
        BaseWrapper baseWrappers=new BaseWrapper();
        baseWrappers.setStatus(true);
        int total=gridEventMapper.deleteByPrimaryKey(eventId);
        if(total>0){
            baseWrappers.setFlag(true);
            baseWrappers.setMsg("ok");

            return  baseWrappers;
        }else {
            baseWrappers.setFlag(true);
            baseWrappers.setMsg("ok");

            return  baseWrappers;
        }
    }
    public BaseWrapper updateByPrimaryKeySelective(GridEvent gridEvent, String dateTime){
        BaseWrapper baseWrappers=new BaseWrapper();
        baseWrappers.setStatus(true);
        if(!StringUtils.checkNull(dateTime)){
            gridEvent.setEventTime(DateFormat.getDate(dateTime));
        }
        int total=gridEventMapper.updateByPrimaryKeySelective(gridEvent);
        if(total>0){
            baseWrappers.setFlag(true);
            baseWrappers.setMsg("ok");
            baseWrappers.setData(gridEvent);
            return  baseWrappers;
        }else {
            baseWrappers.setFlag(true);
            baseWrappers.setMsg("ok");

            return  baseWrappers;
        }
    }



    /**
     * 代办 已办
     * @param request
     * @param page
     * @param pageSize
     * @param prcsFlag
     * @return
     */
    public BaseWrappers selWillGridSendOrReceive(HttpServletRequest request, String eventType, Integer page, Integer pageSize, Integer prcsFlag, Integer parting) {
        BaseWrappers wrappers = new BaseWrappers();
        wrappers.setStatus(true);
        wrappers.setFlag(false);

        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        if (StringUtils.checkNull(users.getUserId())) {
            wrappers.setMsg("用户信息过期");
            return wrappers;
        }


        page = (page - 1) * pageSize;
        Map<String, Object> param = new HashMap<String, Object>();
        if(eventType==null){
            param.put("userId", users.getUserId());
        }
        param.put("eventType",eventType);
        param.put("page", page);
        param.put("pageSize", pageSize);
        param.put("prcsFlag", prcsFlag);
        param.put("parting", parting);


     List<GridEventFlowRunPrcs>  datas=gridEventMapper.selWillGridSendOrReceive(param);
     if(datas!=null&&datas.size()>0){
         for(GridEventFlowRunPrcs gridEvent:datas){
             // 超时计算方法  0-本步骤接收后开始计时,1-上一步骤转交后开始计时
             if (gridEvent.getTimeOutType().equals("0")) {
                 // 判断该步骤状态  1-未接收,2-办理中,3-转交下一步，下一步经办人无人接收,4-已办结,5-自由流程预设步骤,6-已挂起
                 if (gridEvent.getPrFlag().equals(2)) {
                     if (!StringUtils.checkNull(gridEvent.getPrcsTime())) {
                         setTimeOutStr(gridEvent, DateFormat.getTime(gridEvent.getPrcsTime()));
                     }
                 } else if (gridEvent.getPrFlag().equals(1)) {
                     gridEvent.setIfOutTime(true);
                     gridEvent.setTimeOutStr("计时未开始");
                 }
             } else if (gridEvent.getTimeOutType().equals("1")) {
                 if (!gridEvent.getRealPrcsId().equals(1)) {
                     if (!gridEvent.getPrFlag().equals(3) && !gridEvent.getPrFlag().equals(4)) {
                         if (!StringUtils.checkNull(gridEvent.getCreateTime())) {
                             setTimeOutStr(gridEvent, DateFormat.getTime(gridEvent.getCreateTime()));
                         }
                     } else if (gridEvent.getPrFlag().equals(3)) {
                         gridEvent.setTimeOutStr("计时已结束");
                         gridEvent.setIfOutTime(true);
                     } else if (gridEvent.getPrFlag().equals(4)) {
                         gridEvent.setTimeOutStr("计时已结束");
                         gridEvent.setIfOutTime(true);
                     }
                 }
             }
             SysCode singleCode = sysCodeMapper.getSingleCode(gridEvent.getEventTypeParent(), gridEvent.getEventType());
             if(singleCode!=null){
                 gridEvent.setCodeName(singleCode.getCodeName());
             }
             gridEvent.setDateTime(DateFormat.getStrDate(gridEvent.getEventTime()));
             if(gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId())!=null){
                 gridEvent.setGridName(gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId()).getGridName());
             }
         }
     }

     Integer total=gridEventMapper.selWillGridSendOrReceiveCounts(param);
        wrappers.setTotal(total);
        wrappers.setFlag(true);
        wrappers.setDatas(datas);
        return wrappers;
    }

    /**
     * 转交 保存列表
     * @param request
     * @param page
     * @param pageSize
     * @param flowStatus
     * @return
     */
    public BaseWrappers selectbYfLOWstuts(HttpServletRequest request, boolean useFlag, Integer page, Integer pageSize, String flowStatus, Integer parting){
        BaseWrappers wrappers = new BaseWrappers();
        wrappers.setStatus(true);
        wrappers.setFlag(false);
        int total=0;
        List<GridEvent>  datas=new ArrayList<GridEvent>();
        List<String> userIdList = new ArrayList<>();
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        if (StringUtils.checkNull(users.getUserId())) {
            wrappers.setMsg("用户信息过期");
            return wrappers;
        }
        if (StringUtils.checkNull(flowStatus)){
            wrappers.setMsg("err");
            return wrappers;
        }
        PageParams pageParams =new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("pageParams",pageParams);
       // param.put("userId", users.getUserId());
        param.put("flowStatus", flowStatus);
        param.put("parting", parting);

        if(flowStatus.equals("0")){  //查询未上报的
            param.put("userId", users.getUserId());
            datas = gridEventMapper.selectbYfLOWstuts(param);
            param.remove("pageParams");
            total= gridEventMapper.selectbYfLOWstutsCounts(param);
        }else {   //查询上报事件
            //根据userID查询该角色可以查看的巡检记录
            //先查询该角色是否有查询所有网格巡检权限
            if(users.getUserPriv()!=null){
                if(users.getUserPriv()==1 || users.getUserPriv()==2 || users.getUserPriv()==3 || users.getUserPriv()==6 || users.getUserPriv()==13 || users.getUserPriv()==93){
                    //查询全部权限角色
                    datas = gridEventMapper.selectbYfLOWstuts(param);
                    param.remove("pageParams");
                    total= gridEventMapper.selectbYfLOWstutsCounts(param);
                }else {
                    //查询自己社区权限
                    //计数器
                    List<String> stringUserId=gridInfoMapper.sQqXselect(users.getUserId());
                    if(stringUserId.size()==0){
                        userIdList.add(users.getUserId());
                        param.put("StringUser",userIdList);
                        datas = gridEventMapper.selectbYfLOWstuts(param);
                        param.remove("pageParams");
                        total= gridEventMapper.selectbYfLOWstutsCounts(param);
                    }else {
                        String tmesim="";
                        for(int cou=0;cou<stringUserId.size();cou++){
                            if(stringUserId.get(cou)!=null){
                                if(!stringUserId.get(cou).toString().equals("''")){
                                    tmesim+=stringUserId.get(cou);
                                }
                            }
                        }
                        String[] stringList=null;
                        if(tmesim!=null){stringList = tmesim.split(",");
                        }
                        //数组去重复
                        List<String> list = new ArrayList<>();
                        for(int i=0;i<stringList.length;i++){
                            for(int j=i+1;j<stringList.length;j++){
                                if(stringList[i].equals(stringList[j])){
                                    j = ++i;
                                }
                            }
                            list.add(stringList[i]);
                        }
//                        //将uid转换成UserID
//                        for(String uid:list){
//
//                        }
                        param.put("StringUser",list);
                        datas = gridEventMapper.selectbYfLOWstuts(param);
                        param.remove("pageParams");
                        total= gridEventMapper.selectbYfLOWstutsCounts(param);
                    }
                }
            }else {
                param.put("userId", users.getUserId());
                datas = gridEventMapper.selectbYfLOWstuts(param);
                param.remove("pageParams");
                total= gridEventMapper.selectbYfLOWstutsCounts(param);
            }
        }
       // List<GridEvent>  datas=gridEventMapper.selectbYfLOWstuts(param);
        if(datas!=null&&datas.size()>0) {
            for (GridEvent gridEvent : datas) {
//设置提交人姓名
                gridEvent.setUserName(usersMapper.getUsernameByUserId(gridEvent.getUserId()));
                //设置社区名称
                GridInfo gridInfo = gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId());
                if (gridInfo != null) {
                    gridEvent.setGridName(gridInfo.getGridName());
                } else {
                    gridEvent.setGridName("");
                }
                if (gridEvent.getRumId() != 0) {
                    FlowRunPrcs flowRunPrcs = flowRunPrcsMapper.queryByRunId(gridEvent.getRumId());
                    if (flowRunPrcs != null) {

                        gridEvent.setPrcsId(flowRunPrcs.getPrcsId().toString());
                    } else {

                        gridEvent.setPrcsId("");
                    }
                }
                gridEvent.setDateTime(DateFormat.getStrDate(gridEvent.getEventTime()));
                SysCode code = sysCodeMapper.getSingleCode(gridEvent.getEventTypeParent(), gridEvent.getEventType());
                if (code != null) {
                    gridEvent.setCodeName(code.getCodeName());
                }

            }
        }

        wrappers.setFlag(true);
        wrappers.setDatas(datas);
        wrappers.setTotal(total);
        return wrappers;
    }

    public BaseWrappers selectGridEvent(HttpServletRequest request, HttpServletResponse response, Integer page, Integer pageSize, String userId,
                                        String gridId, String exentType, String flowStatus, String beginDate, String endDate, String exportId, Integer parting){
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class,new Users());
        BaseWrappers wrappers = new BaseWrappers();
        List<GridEvent> gridEvents=new ArrayList<GridEvent>();
        List<String> userIdList = new ArrayList<>();
        wrappers.setStatus(true);
        wrappers.setFlag(false);
        page = (page - 1) * pageSize;
        Map<String, Object> param = new HashMap<String, Object>();
       param.put("uid",userId);
       if(1!= user.getUid()){
           param.put("userId", user.getUid());
       }
        param.put("page", page);
        param.put("pageSize", pageSize);
        param.put("gridId", gridId);
        param.put("exentType", exentType);
        param.put("flowStatus", "1");
        param.put("beginDate", beginDate);
        param.put("endDate", endDate);
        param.put("parting", parting);
        //根据userID查询该角色可以查看的巡检记录
        //先查询该角色是否有查询所有网格巡检权限

        param.put("StringUser",userIdList);
        gridEvents = gridEventMapper.selectGridEvent(param);
        int total = gridEventMapper.selectGridEventCount(param);

        if("1".equals(exportId)){
            if(gridEvents!=null&&gridEvents.size()>0){
                for(GridEvent gridEvent:gridEvents){
                    //设置提交人姓名
                    gridEvent.setUserName(usersMapper.getUsernameByUserId(gridEvent.getUserId()));
                    //设置社区名称
                    GridInfo gridInfo = gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId());
                    String upperGrid=gridInfo.getUpperGrid();
//                    if(Integer.parseInt(upperGrid)!=0){
//                        GridInfo gridInfo1=gridInfoMapper.selectByPrimaryKey(Integer.parseInt(upperGrid));
//                        gridEvent.setGridName(gridInfo1.getGridName());
//
//                    }else{
                        gridEvent.setGridName(gridInfo.getGridName());

//                    }
                    /*if(gridInfo!=null){
                        gridEvent.setGridName(gridInfo.getGridName());
                    }else{
                        gridEvent.setGridName("");
                    }*/
                    if(gridEvent.getRumId()!=0) {
                        FlowRunPrcs flowRunPrcs = flowRunPrcsMapper.queryByRunId(gridEvent.getRumId());
                        if (flowRunPrcs != null) {

                            gridEvent.setPrcsId(flowRunPrcs.getFlowPrcs().toString());
                            int flowprcs= Integer.parseInt(flowRunPrcs.getParent())+1;
                            gridEvent.setFlowPrcs(flowprcs+"");
                        } else {

                            gridEvent.setPrcsId("");
                            gridEvent.setFlowPrcs("");
                        }
                    }
                    gridEvent.setDateTime(DateFormat.getStrDate(gridEvent.getEventTime()));
                    SysCode singleCode = sysCodeMapper.getSingleCode(gridEvent.getEventTypeParent(), gridEvent.getEventType());
                    if(singleCode!=null){
                        gridEvent.setCodeName(singleCode.getCodeName());
                    }

                }
            }

//        Integer total=gridEventMapper.selectGridEventCount(param);
        wrappers.setDatas(gridEvents);
        wrappers.setTotal(total);
        wrappers.setFlag(true);
        wrappers.setMsg("ok");

        }else {
            try {
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("事件处理", 9);
                String[] secondTitles = {"社区id", "提交时间", "事件描述"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);

                // String[] beanProperty = {user.getDep().getDeptName(),"userName","userPrivName", "roleAuxiliaryName","online","sex","online","telNoDept","telNoDept","departmentPhone","email"};
                String[] beanProperty = {"gridId", "eventTime", "eventInfo"};

                HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, gridEvents, beanProperty);
                ServletOutputStream out = response.getOutputStream();

                String filename = "事件处理导出.xls";
                filename = FileUtils.encodeDownloadFilename(filename,
                        request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("content-disposition",
                        "attachment;filename=" + filename);
                workbook.write(out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return wrappers;
    }


    public BaseWrappers getStatistics(String timeType, String queryType, Integer parting) {
        BaseWrappers baseWrappers = new BaseWrappers();
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (timeType.equals("day")) {
            long current = System.currentTimeMillis();//当前时间毫秒数
            long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
            long twelve = zero + 24 * 60 * 60 * 1000 - 1;//今天23点59分59秒的毫秒数
            map.put("startTime", sdf.format(zero));
            map.put("endTime", sdf.format(twelve));
        } else if (timeType.equals("week")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            // 获取星期一
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            long mondayTime = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
            // 获取星期日
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            long sundayTime = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1;

            map.put("startTime", sdf.format(mondayTime));
            map.put("endTime", sdf.format(sundayTime));
        } else if (timeType.equals("month")) {
            Calendar c = Calendar.getInstance();
            //设置为1号,当前日期既为本月第一天
            c.set(Calendar.DAY_OF_MONTH, 1);
            String first = sdf.format(c.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset());
            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = sdf.format(ca.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1);
            map.put("startTime", first);
            map.put("endTime", last);
        } else if (timeType.equals("year")) {
            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            long first = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();

            calendar.set(Calendar.MONTH, 12);
            calendar.set(Calendar.DAY_OF_MONTH, -1);
            long last = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1;

            map.put("startTime", sdf.format(first));
            map.put("endTime", sdf.format(last));
        }
        try {
            List<StatisticsModel> gridChecks = null;
            if ("grid".equals(queryType)) {
                map.put("field", "grid_id");
                map.put("parting", parting);
                List<GridInfo> gridInfos =  gridInfoMapper.gridSettingsTree("1");
                gridChecks = gridEventMapper.getStatisticsByGrid(map);
                if(gridInfos.size()!=gridChecks.size()){
                    for (GridInfo g:gridInfos){
                        Integer gridId = g.getGridId();
                        for(int i=0;i<gridChecks.size()+1;i++){
                            if(i<gridChecks.size()){
                                if(gridId.equals(gridChecks.get(i).getId())){
                                    break;
                                }
                            }else {
                                StatisticsModel statisticsModel = new StatisticsModel();
                                statisticsModel.setId(gridId.toString());
                                statisticsModel.setY(0);
                                statisticsModel.setName(g.getGridName());
                                statisticsModel.setData(new ArrayList<StatisticsModel>());
                                gridChecks.add(statisticsModel);
                                break;
                            }
                            }
                        }

                }
                for (StatisticsModel s : gridChecks) {
                    map.put("field", "user_id");
                    map.put("gridId", s.getId());
                    List<StatisticsModel> statisticsModels = gridEventMapper.getStatisticsByGrid(map);
                    s.setData(statisticsModels);
                }
            } else {
                map.put("field", "event_type_parent");
                map.put("parting", parting);
                Map<String,String> sysCodes = new HashMap<String,String>();
                sysCodes.put("appearance","市容环境");
                sysCodes.put("bazaar","秩序环境");
                sysCodes.put("livelihood","设施环境");
                sysCodes.put("zoology"," 生态环境");
                sysCodes.put("protection","环保环境");

                Set<String> strings = sysCodes.keySet();

                gridChecks = gridEventMapper.getStatisticsByType(map);
                if(sysCodes.size()!=gridChecks.size()){
                    for (String codeNo:strings){
                        for(int i=0;i<gridChecks.size()+1;i++){
                            if(i<gridChecks.size()){
                                if(codeNo.equals(gridChecks.get(i).getId().toString())){
                                    break;
                                }
                            }else {
                                StatisticsModel statisticsModel = new StatisticsModel();
                                statisticsModel.setId(codeNo);
                                statisticsModel.setY(0);
                                statisticsModel.setName(sysCodes.get(codeNo));
                                statisticsModel.setData(new ArrayList<StatisticsModel>());
                                gridChecks.add(statisticsModel);
                                break;
                            }

                        }
                    }
                }
                for (StatisticsModel s : gridChecks) {
                    map.put("field", "event_type");
                    map.put("eventTypeParent", s.getId());
                    for (String codeNo:strings) {
                        if(s.getId().toString().equals(codeNo)){
                            s.setName(sysCodes.get(codeNo));
                        }
                    }
                    List<SysCode> sysCode = sysCodeMapper.getSysCode(s.getId().toString());

                    List<StatisticsModel> statisticsModels = gridEventMapper.getStatisticsByType(map);
                    for (StatisticsModel statisticsModel:statisticsModels) {
                        for (SysCode s2:sysCode) {
                            if (s2.getCodeNo().equals(statisticsModel.getName())){
                                statisticsModel.setName(s2.getCodeName());
                            }
                        }
                    }
                    s.setData(statisticsModels);
                }
            }
            for(int t=gridChecks.size()-1;t>=0;t--){
                if(gridChecks.get(t).getName()!=null && gridChecks.get(t).getName()!=""){
                    if(gridChecks.get(t).getName().indexOf("网格")!=-1){
                        if(gridChecks.get(t).getId()!=null){
                            GridInfo gridInfo = gridInfoMapper.selectByPrimaryKey(Integer.valueOf(gridChecks.get(t).getId()));
                            String upperGrid=gridInfo.getUpperGrid();
                            if(Integer.parseInt(upperGrid)!=0){
                                GridInfo gridInfo1=gridInfoMapper.selectByPrimaryKey(Integer.parseInt(upperGrid));
                                gridChecks.get(t).setName(gridInfo1.getGridName());
                            }else{
                                gridChecks.get(t).setName(gridInfo.getGridName());
                            }
                        }
                    }
                }
            }
            baseWrappers.setFlag(true);
            baseWrappers.setStatus(true);
            baseWrappers.setMsg("ok");
            baseWrappers.setDatas(gridChecks);
        } catch (Exception e) {
            e.printStackTrace();
            baseWrappers.setFlag(false);
            baseWrappers.setMsg("err");
        }
        return baseWrappers;
    }

    public BaseWrappers getCountByname(String timeType, Integer parting){
        BaseWrappers baseWrappers = new BaseWrappers();
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (timeType.equals("day")) {
            long current = System.currentTimeMillis();//当前时间毫秒数
            long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
            long twelve = zero + 24 * 60 * 60 * 1000 - 1;//今天23点59分59秒的毫秒数
            map.put("startTime", sdf.format(zero));
            map.put("endTime", sdf.format(twelve));
        } else if (timeType.equals("week")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            // 获取星期一
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            long mondayTime = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
            // 获取星期日
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            long sundayTime = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1;

            map.put("startTime", sdf.format(mondayTime));
            map.put("endTime", sdf.format(sundayTime));
        } else if (timeType.equals("month")) {
            Calendar c = Calendar.getInstance();
            //设置为1号,当前日期既为本月第一天
            c.set(Calendar.DAY_OF_MONTH, 1);
            String first = sdf.format(c.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset());
            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = sdf.format(ca.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1);
            map.put("startTime", first);
            map.put("endTime", last);
        } else if (timeType.equals("year")) {
            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            long first = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();

            calendar.set(Calendar.MONTH, 12);
            calendar.set(Calendar.DAY_OF_MONTH, -1);
            long last = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1;

            map.put("startTime", sdf.format(first));
            map.put("endTime", sdf.format(last));
        }
        map.put("parting", parting);
        List<StatisticsModel> gridChecks = gridEventMapper.getCountByname(map);
        int total=gridChecks.get(0).getY();
        baseWrappers.setFlag(true);
        baseWrappers.setStatus(true);
        baseWrappers.setMsg("ok");
        baseWrappers.setTotal(total);
        baseWrappers.setDatas(gridChecks);
        return  baseWrappers;
    }

    public BaseWrappers getLastEventMsg(){
        BaseWrappers baseWrappers = new BaseWrappers();
        List<GridEvent> lastEventMsg = gridEventMapper.getLastEventMsg();
        if (lastEventMsg!=null&&lastEventMsg.size()>0){
            for (GridEvent gridEvent:lastEventMsg){
                SysCode singleCode = sysCodeMapper.getSingleCode(gridEvent.getEventTypeParent(), gridEvent.getEventType());
                if(singleCode!=null){
                    gridEvent.setCodeName(singleCode.getCodeName());
                }
                gridEvent.setDateTime(DateFormat.getStrDate(gridEvent.getEventTime()));
            }
        }
        baseWrappers.setDatas(lastEventMsg);
        baseWrappers.setFlag(true);
        baseWrappers.setStatus(true);
        return baseWrappers;
    }

    private void setTimeOutStr(GridEventFlowRunPrcs d, Integer time) {
        if (!StringUtils.checkNull(d.getTimeOut())) {
            // 获取限时时间和类型 hour是小时 day是天
            String[] split = d.getTimeOut().split(",");
            if (split.length == 2) {
                String timeCount = split[0];
                String timeType = split[1];
                // 判断是哪种类型的时间限制
                if (timeType.equals("hour")) {
                    // 总计办理时间时间戳
                    int i = Integer.valueOf(timeCount) * 60 * 60;
                    // 超时时间
                    int outTimeEnd = time + i;
                    // 当前时间
                    int currentTime = (int) (System.currentTimeMillis() / 1000);
                    // 获取时间差 计算时间 并赋值
                    d.setTimeOutStr("时限" + timeCount + "小时，" + getTimeOutStr(outTimeEnd, currentTime, d));
                } else if (timeType.equals("day")) {
                    // 总计办理时间时间戳
                    int i = Integer.valueOf(timeCount) * 24 * 60 * 60;
                    // 超时时间
                    int outTimeEnd = time + i;
                    // 当前时间
                    int currentTime = (int) (System.currentTimeMillis() / 1000);
                    // 获取时间差 计算时间 并赋值
                    d.setTimeOutStr("时限" + timeCount + "天，" + getTimeOutStr(outTimeEnd, currentTime, d));
                }
            }
        }
    }

    /**
     * 作者: 张航宁
     * 日期: 2017/9/20
     * 说明: 获取剩余或者超时时间
     */
    private String getTimeOutStr(int outTimeEnd, int currentTime, GridEventFlowRunPrcs d) {
        String s;
        if (outTimeEnd > currentTime) {
            int times = outTimeEnd - currentTime;
            s = "剩余" + DateFormat.returnTime(times);
            d.setIfOutTime(false);
        } else {
            int times = currentTime - outTimeEnd;
            s = "超时" + DateFormat.returnTime(times);
            d.setIfOutTime(true);
        }
        return s;
    }

    /**
     * 方法说明：手机端事件统计接口
     * 创建时间：2017-12-12
     * 创建人：陈志才
     * @return
     */
    public ToJson getStatistics(String timeType, Integer parting, Integer flowId){
            ToJson json=new ToJson();
            Map<String,Object> maplist=new HashMap<String,Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (timeType.equals("day")) {
            long current = System.currentTimeMillis();//当前时间毫秒数
            long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
            long twelve = zero + 24 * 60 * 60 * 1000 - 1;//今天23点59分59秒的毫秒数
            map.put("startTime", sdf.format(zero));
            map.put("endTime", sdf.format(twelve));
        } else if (timeType.equals("week")) {
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            // 获取星期一
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            long mondayTime = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
            // 获取星期日
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            long sundayTime = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1;

            map.put("startTime", sdf.format(mondayTime));
            map.put("endTime", sdf.format(sundayTime));
        } else if (timeType.equals("month")) {
            Calendar c = Calendar.getInstance();
            //设置为1号,当前日期既为本月第一天
            c.set(Calendar.DAY_OF_MONTH, 1);
            String first = sdf.format(c.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset());
            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = sdf.format(ca.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1);
            map.put("startTime", first);
            map.put("endTime", last);
        } else if (timeType.equals("year")) {
            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            long first = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();

            calendar.set(Calendar.MONTH, 12);
            calendar.set(Calendar.DAY_OF_MONTH, -1);
            long last = calendar.getTime().getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1;

            map.put("startTime", sdf.format(first));
            map.put("endTime", sdf.format(last));
        }
        try{
            map.put("field", "event_type_parent");
            map.put("parting",parting);
            Map<String,String> sysCodes = new HashMap<String,String>();
            sysCodes.put("appearance","市容环境");
            sysCodes.put("bazaar","秩序环境");
            sysCodes.put("livelihood","设施环境");
            sysCodes.put("zoology"," 生态环境");
            sysCodes.put("protection","环保环境");

            Set<String> strings = sysCodes.keySet();
            List<StatisticsModel> gridChecks = null;
            gridChecks = gridEventMapper.getStatisticsByType(map);
            if(sysCodes.size()!=gridChecks.size()){
                for (String codeNo:strings){
                    for(int i=0;i<gridChecks.size()+1;i++){
                        if(i<gridChecks.size()){
                            if(codeNo.equals(gridChecks.get(i).getId().toString())){
                                break;
                            }
                        }else {
                            StatisticsModel statisticsModel = new StatisticsModel();
                            statisticsModel.setId(codeNo);
                            statisticsModel.setY(0);
                            statisticsModel.setName(sysCodes.get(codeNo));
                            statisticsModel.setData(new ArrayList<StatisticsModel>());
                            gridChecks.add(statisticsModel);
                            break;
                        }

                    }
                }
            }
            for (StatisticsModel s : gridChecks) {
                map.put("field", "event_type");
                map.put("eventTypeParent", s.getId());
                for (String codeNo:strings) {
                    if(s.getId().toString().equals(codeNo)){
                        s.setName(sysCodes.get(codeNo));
                    }
                }
                List<SysCode> sysCode = sysCodeMapper.getSysCode(s.getId().toString());

                List<StatisticsModel> statisticsModels = gridEventMapper.getStatisticsByType(map);
                for (StatisticsModel statisticsModel:statisticsModels) {
                    for (SysCode s2:sysCode) {
                        if (s2.getCodeNo().equals(statisticsModel.getName())){
                            statisticsModel.setName(s2.getCodeName());
                        }
                    }
                }
                s.setData(statisticsModels);
            }
            //扇形图
            if(timeType.equals("day")){   //今天
                int sum= fixFormMapper.selectFlowCountsAz(flowId); //全部
                int NotTime=fixFormMapper.selectFlowCountsAzNo(flowId); //未超时
                maplist.put("outTime",sum-NotTime);
                maplist.put("notTime",NotTime);
            }else if(timeType.equals("week")){//本周
                int sum= fixFormMapper.selectFlowCountsAz(flowId); //全部
                int NotTime=fixFormMapper.selectFlowCountsAzWeek(flowId); //未超时
                maplist.put("outTime",sum-NotTime);
                maplist.put("notTime",NotTime);  
            }else if(timeType.equals("month")){ //本月
                int sum= fixFormMapper.selectFlowCountsAz(flowId); //全部
                int NotTime=fixFormMapper.selectFlowCountsAzMoth(flowId); //未超时
                maplist.put("outTime",sum-NotTime);
                maplist.put("NotTime",NotTime);
            }
            maplist.put("gridChecks",gridChecks);
            json.setObject(maplist);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
        }
            return json;
    }

    public BaseWrappers selecTd(HttpServletRequest request, String timeUser){
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        BaseWrappers baseWrappers = new BaseWrappers();
        List<StatisticsModel> gridChecks = null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("field", "event_type_parent");
        map.put("parting", "1");
        map.put("timeUser",timeUser);
        map.put("userId",user.getUserId());
        Map<String,String> sysCodes = new HashMap<String,String>();
        sysCodes.put("appearance","矛盾纠纷类");
        sysCodes.put("bazaar","治安问题类");
        sysCodes.put("livelihood","影响经济秩序类");
        sysCodes.put("zoology"," 安全生产隐患类");
        sysCodes.put("protection","公共安全隐患类");

        Set<String> strings = sysCodes.keySet();
            if(timeUser!=null){
            gridChecks = gridEventMapper.getStatisticsByTypetis(map);
            }else {
            gridChecks = gridEventMapper.getStatisticsByType(map);
        }

        if(sysCodes.size()!=gridChecks.size()){
            for (String codeNo:strings){
                for(int i=0;i<gridChecks.size()+1;i++){
                    if(i<gridChecks.size()){
                        if(codeNo.equals(gridChecks.get(i).getId().toString())){
                            break;
                        }
                    }else {
                        StatisticsModel statisticsModel = new StatisticsModel();
                        statisticsModel.setId(codeNo);
                        statisticsModel.setY(0);
                        statisticsModel.setName(sysCodes.get(codeNo));
                        statisticsModel.setData(new ArrayList<StatisticsModel>());
                        gridChecks.add(statisticsModel);
                        break;
                    }

                }
            }
        }
        for (StatisticsModel s : gridChecks) {
            map.put("field", "event_type");
            map.put("eventTypeParent", s.getId());
            for (String codeNo:strings) {
                if(s.getId().toString().equals(codeNo)){
                    s.setName(sysCodes.get(codeNo));
                }
            }
            List<SysCode> sysCode = sysCodeMapper.getSysCode(s.getId().toString());
            List<StatisticsModel> statisticsModels=null;
            if(timeUser!=null){
                statisticsModels = gridEventMapper.getStatisticsByTypetis(map);
            }else {
                 statisticsModels = gridEventMapper.getStatisticsByType(map);
            }

            for (StatisticsModel statisticsModel:statisticsModels) {
                for (SysCode s2:sysCode) {
                    if (s2.getCodeNo().equals(statisticsModel.getName())){
                        statisticsModel.setName(s2.getCodeName());
                    }
                }
            }
            s.setData(statisticsModels);
        }
        for(int t=0;t<gridChecks.size();t++){
            if(gridChecks.get(t).getName()!=null && gridChecks.get(t).getName()!=""){
                if(gridChecks.get(t).getName().contains("网格")){
                    GridInfo gridInfo1=gridInfoMapper.selectByPrimaryKey(Integer.parseInt(gridChecks.get(t).getId()));
                    if(gridInfo1!=null){
                        gridChecks.get(t).setName(gridInfo1.getGridName());
                    }
                }
            }
        }
        baseWrappers.setFlag(true);
        baseWrappers.setStatus(true);
        baseWrappers.setMsg("ok");
        baseWrappers.setDatas(gridChecks);
        return  baseWrappers;
    }


    public ToJson selectEventWyhwg(Integer eventId){
             ToJson json=new ToJson();
             try{
                   Integer eventWyhwg=gridEventMapper.selectEventWyhwg(eventId);
                   Map<String,Object> map=new HashMap<String,Object>();
                   map.put("eventWyhwg",eventWyhwg);
                   json.setObject(map);
                   json.setFlag(0);
                   json.setMsg("ok");
             }catch (Exception e){
                 e.printStackTrace();
             }
             return json;
    }

    public BaseWrappers getStatisticsByGridType(String timeType, String queryType, String startTime, String endTime, HttpServletRequest request) {
        BaseWrappers baseWrapper=new BaseWrappers();
        if("2".equals(queryType)){
            Map<String,Object> map=new HashMap<String,Object>();
                map.put("timeType",timeType);
               map.put("startTime",startTime);
                map.put("endTime",endTime);
                List<StatisticsModel> statisticsModels=  gridEventMapper.getStatisticsByCodeTypeBreak(map);
               baseWrapper.setDatas(statisticsModels);

        }else {
            Map<String,Object> map=new HashMap<String,Object>();
            List<Department> departmentList=departmentMapper.getDeptIdGridByParent("27");
            List<StatisticsModel> statisticsModelList=new ArrayList<StatisticsModel>();
            for (Department department:departmentList){
                StatisticsModel statisticsModel=new StatisticsModel();
                statisticsModel.setName(department.getDeptName());
                statisticsModel.setId(String.valueOf(department.getDeptId()));
                map.put("timeType",timeType);
                map.put("startTime",startTime);
                map.put("endTime",endTime);
                List<StatisticsModel> statisticsModels=  gridEventMapper.getStatisticsByGridTypeBreak(map);
                int total=0;
                for(int i=0;i<statisticsModels.size();i++){
                    if(department.getDeptName().equals(statisticsModels.get(i).getName())){
                        total+= statisticsModels.get(i).getY();
                        statisticsModel.setData(statisticsModels);
                        statisticsModel.setY(total);
                    }else {
                        statisticsModels.get(i).setName(department.getDeptName());
                        statisticsModel.setData(statisticsModels);
                        statisticsModel.setY(total);
                    }
                }
                statisticsModel.setY(total);
                statisticsModelList.add(statisticsModel);
            }
            baseWrapper.setDatas(statisticsModelList);
        }
        baseWrapper.setStatus(true);
        baseWrapper.setFlag(true);
        return baseWrapper;
    }


    public BaseWrappers getStatisticsByGridEventType(String timeType, String queryType, String startTime, String endTime, HttpServletRequest request, String typeDate) {
        BaseWrappers baseWrapper=new BaseWrappers();
        if("2".equals(queryType)){
            List<SysCode> sysCodes = sysCodeMapper.selectBT();
            List<StatisticsModel> statisticsModelList=new ArrayList<StatisticsModel>();
            for(SysCode s:sysCodes){
                StatisticsModel statisticsModel=new StatisticsModel();
                statisticsModel.setName(s.getCodeName());
                if(s.getCodeId()==6756){
                    statisticsModel.setId("appearance");
                } else if(s.getCodeId()==6757){
                    statisticsModel.setId("bazaar");
                }else if(s.getCodeId()==6758){
                    statisticsModel.setId("livelihood");
                }else if(s.getCodeId()==6759){
                    statisticsModel.setId("zoology");
                }else if(s.getCodeId()==6760){
                    statisticsModel.setId("protection");
                }else {
                    statisticsModel.setId(String.valueOf(s.getCodeId()));
                }
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("timeType",timeType);
                map.put("type",s.getCodeNo());
                map.put("startTime",startTime);
                map.put("endTime",endTime);
                map.put("typeDate",typeDate);
                List<StatisticsModel> statisticsModels=  gridEventMapper.getStatisticsByGridCodeType(map);
                int total=0;
                for(int i=0;i<statisticsModels.size();i++){
                    total+= statisticsModels.get(i).getY();
                }
                statisticsModel.setData(statisticsModels);
                statisticsModel.setY(total);
                statisticsModelList.add(statisticsModel);

            }
            baseWrapper.setDatas(statisticsModelList);

        }else {
            List<Department> departmentList=departmentMapper.getDeptIdGridByParent("0");
            List<StatisticsModel> statisticsModelList=new ArrayList<StatisticsModel>();
            for (Department department:departmentList){
                StatisticsModel statisticsModel=new StatisticsModel();
                statisticsModel.setName(department.getDeptName());
                statisticsModel.setId(String.valueOf(department.getDeptId()));
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("timeType",timeType);
                map.put("depId",department.getDeptId());
                map.put("startTime",startTime);
                map.put("endTime",endTime);
                List<StatisticsModel> statisticsModels=  gridEventMapper.getStatisticsByGridEventType(map);
                int total=0;
                for(int i=0;i<statisticsModels.size();i++){
                    total+= statisticsModels.get(i).getY();
                }
                statisticsModel.setData(statisticsModels);
                statisticsModel.setY(total);
                statisticsModelList.add(statisticsModel);
            }
            baseWrapper.setDatas(statisticsModelList);
        }

        baseWrapper.setStatus(true);
        baseWrapper.setFlag(true);
        return baseWrapper;
    }


    public BaseWrappers gridByUserName(String userName, String location, Integer page, Integer pageSize, String gridId, String type, String timeType){
        BaseWrappers baseWrapper=new BaseWrappers();
        page = (page - 1) * pageSize;
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("userId", userName);
        param.put("page", page);
        param.put("pageSize", pageSize);
        param.put("timeType",timeType);
        param.put("location", "2");
        param.put("gridId", gridId);
        param.put("type", type);
        List<GridEvent> gridEventList=gridEventMapper.gridEventByUserNameBreak(param);
        if(gridEventList!=null&&gridEventList.size()>0){
            for(GridEvent gridEvent:gridEventList){
//设置提交人姓名
            gridEvent.setUserName(usersMapper.getUsernameByUserId(gridEvent.getUserId()));
            //设置社区名称
            GridInfo gridInfo = gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId());
            if(gridInfo!=null){
                gridEvent.setGridName(gridInfo.getGridName());
            }else{
                gridEvent.setGridName("");
            }
            if(gridEvent.getRumId()!=0) {
                FlowRunPrcs flowRunPrcs = flowRunPrcsMapper.queryByRunId(gridEvent.getRumId());
                if (flowRunPrcs != null) {

                    gridEvent.setPrcsId(flowRunPrcs.getPrcsId().toString());
                } else {

                    gridEvent.setPrcsId("");
                }
            }
            gridEvent.setDateTime(DateFormat.getStrDate(gridEvent.getEventTime()));
            gridEvent.setCodeName(sysCodeMapper.getSingleCode(gridEvent.getEventTypeParent(),gridEvent.getEventType()).getCodeName());
        }
        }
        Integer total = gridEventMapper.gridEventByUserNameCountBreak(param);
        baseWrapper.setDatas(gridEventList);
        baseWrapper.setTotal(total);
        baseWrapper.setStatus(true);
        baseWrapper.setFlag(true);
        return baseWrapper;
    }

    public BaseWrappers gridEventByUserName(String timeType, String startTime, String endTime, String userName, String location, Integer page, Integer pageSize, String gridId, String type){
        BaseWrappers baseWrapper=new BaseWrappers();
        page = (page - 1) * pageSize;
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("userId", userName);
        param.put("startTime",startTime);
        param.put("endTime",endTime);
        param.put("timeType",timeType);
        param.put("page", page);
        param.put("pageSize", pageSize);
        param.put("location", "1");
        param.put("gridId", gridId);
        param.put("type", type);
        List<GridEvent> gridEventList=gridEventMapper.gridEventByUserName(param);
        if(gridEventList!=null&&gridEventList.size()>0){
            for(GridEvent gridEvent:gridEventList){
//设置提交人姓名
                gridEvent.setUserName(usersMapper.getUsernameByUserId(gridEvent.getUserId()));
                //设置社区名称
                GridInfo gridInfo = gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId());
                if(gridInfo!=null){
                    gridEvent.setGridName(gridInfo.getGridName());
                }else{
                    gridEvent.setGridName("");
                }
                if(gridEvent.getRumId()!=0) {
                    FlowRunPrcs flowRunPrcs = flowRunPrcsMapper.queryByRunId(gridEvent.getRumId());
                    if (flowRunPrcs != null) {

                        gridEvent.setPrcsId(flowRunPrcs.getPrcsId().toString());
                    } else {

                        gridEvent.setPrcsId("");
                    }
                }
                gridEvent.setDateTime(DateFormat.getStrDate(gridEvent.getEventTime()));
                gridEvent.setCodeName(sysCodeMapper.getSingleCode(gridEvent.getEventTypeParent(),gridEvent.getEventType()).getCodeName());
            }
        }
        Integer total = gridEventMapper.gridEventByUserNameCount(param);
        baseWrapper.setDatas(gridEventList);
        baseWrapper.setTotal(total);
        baseWrapper.setStatus(true);
        baseWrapper.setFlag(true);
        return baseWrapper;
    }

    /**
     * 穿透
     * @param userId
     * @param gridId
     * @param startTime
     * @param endTime
     * @param eventType
     * @return
     */
    public ToJson sjsbct(String userId, Integer gridId, String startTime, String endTime, String eventType, Integer page, Integer pageSize){
             ToJson json=new ToJson();
             try {
             Map<String,Object> map=new HashMap<String,Object>();
             page=(page-1)*pageSize;
                 map.put("userId",userId);
                 map.put("gridId",gridId);
                 map.put("startTime",startTime);
                 map.put("endTime",endTime);
                 map.put("eventType",eventType);
                 map.put("page",page);
                 map.put("pageSize",pageSize);
                 List<GridEvent> list=gridEventMapper.sjsbct(map);
                 int count=gridEventMapper.sjsbctCount(map);
                 json.setObj(list);
                 json.setObject(count);
                 json.setMsg("ok");
                 json.setFlag(0);
             }catch (Exception e){
                 json.setMsg("err");
                 json.setFlag(0);
                 e.printStackTrace();
             }
             return json;
    }

    /**
    * @Author 程叶同
    * @Description 事件统计按社区或人员排行榜
    * @Date 10:06 2018/1/25
    */
    public BaseWrapper getEventRankList(String ranktype, String timeType, String startTime, String endTime){ //一个月 三个月 半年 一年
        BaseWrapper baseWrapper=new BaseWrapper();
        List<GridEventRankModel> eventRankListByGrid=null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar=Calendar.getInstance();

        Map<String,Object> map=new HashMap<String,Object>();
        if(timeType!=null&&timeType!=""){
            Date start=null;
            Date end=new Date();
            if (timeType.equals("month")) {
                calendar.setTime(end);
                calendar.add(Calendar.MONTH,-1);
                start=calendar.getTime();

            } else if (timeType.equals("threeMonth")) {
                calendar.setTime(end);
                calendar.add(Calendar.MONTH,-3);
                start=calendar.getTime();
            } else if (timeType.equals("halfYear")) {
                calendar.setTime(end);
                calendar.add(Calendar.MONTH,-6);
                start=calendar.getTime();

            } else if (timeType.equals("year")) {
                calendar.setTime(end);
                calendar.add(Calendar.YEAR,-1);
                start=calendar.getTime();
            }
            map.put("startTime", sdf.format(start));
            map.put("endTime", sdf.format(end));
        }else{
            map.put("startTime", startTime);
            map.put("endTime", endTime);
        }

          if(ranktype!=null&&ranktype!=""){
              if("1".equals(ranktype)){
                  eventRankListByGrid = gridEventMapper.getEventRankListByGrid(map);
                  for(GridEventRankModel gr:eventRankListByGrid){
                      if(gr.getGridId()!=null&&gr.getGridId()!=""){
                          GridInfo gridInfo = gridInfoMapper.selectByPrimaryKey(Integer.parseInt(gr.getGridId()));
                          if(Integer.parseInt(gridInfo.getUpperGrid())!=1){
                              GridInfo gridInfo1 = gridInfoMapper.selectByPrimaryKey(Integer.parseInt(gridInfo.getUpperGrid()));
                              if(gridInfo1!=null){
                                  gr.setGridName(gridInfo1.getGridName());
                                  gr.setGridId(String.valueOf(gridInfo1.getGridId()));
                              }
                          }
                      }
                  }
                  //去重
                  for(int i=0;i<eventRankListByGrid.size();i++){
                      for(int j=eventRankListByGrid.size()-1;j>i;j--){
                          String gridId1 = eventRankListByGrid.get(i).getGridId();
                          String gridId2 = eventRankListByGrid.get(j).getGridId();
                          if(Integer.parseInt(gridId1)==Integer.parseInt(gridId2)){
                                  eventRankListByGrid.get(i).setNum(String.valueOf(Integer.parseInt(eventRankListByGrid.get(i).getNum())+Integer.parseInt(eventRankListByGrid.get(j).getNum())));
                                  eventRankListByGrid.remove(j);
                              }


                      }
                  }

              }
              if("2".equals(ranktype)){
                  eventRankListByGrid = gridEventMapper.getEventRankListByUser(map);

              }
              if(eventRankListByGrid!=null&&eventRankListByGrid.size()>0){
                  Collections.sort(eventRankListByGrid, new Comparator<GridEventRankModel>() {
                      @Override
                      public int compare(GridEventRankModel o1, GridEventRankModel o2) {
                          if(Integer.parseInt(o1.getNum())>Integer.parseInt(o2.getNum())){
                              return -1;
                          }
                          if(Integer.parseInt(o1.getNum())==Integer.parseInt(o2.getNum())){
                              return 0;
                          }
                          return 1;
                      }
                  });
                  List<GridEventRankModel> result=new ArrayList<GridEventRankModel>();
                  for(int i=0;i<eventRankListByGrid.size();i++){
                      if(i<10){
                          result.add(eventRankListByGrid.get(i));
                      }
                  }
                  baseWrapper.setData(result);
                  baseWrapper.setMsg("ok");
                  baseWrapper.setFlag(true);
                  baseWrapper.setStatus(true);
              }else{
                  baseWrapper.setData(eventRankListByGrid);
                  baseWrapper.setFlag(true);
                  baseWrapper.setStatus(true);
                  baseWrapper.setMsg("数据为空");
              }
          }
        return baseWrapper;
    }
/**
* @Author 程叶同
* @Description 事件统计排行榜穿透
* @Date 10:47 2018/1/26
*/
    public BaseWrapper rankListDetail(String rankType, Integer page, Integer pageSize, boolean useFlag, String gridId, String userId, String timeType, String startTime, String endTime){
        BaseWrapper baseWrapper=new BaseWrapper();
        List<RankListDetailModel> eventRankListByGrid=null;
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("pageParams",pageParams);
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(timeType!=null&&timeType!=""){
            Date start=null;
            Date end=new Date();
            if (timeType.equals("month")) {
                calendar.setTime(end);
                calendar.add(Calendar.MONTH,-1);
                start=calendar.getTime();

            } else if (timeType.equals("threeMonth")) {
                calendar.setTime(end);
                calendar.add(Calendar.MONTH,-3);
                start=calendar.getTime();
            } else if (timeType.equals("halfYear")) {
                calendar.setTime(end);
                calendar.add(Calendar.MONTH,-6);
                start=calendar.getTime();

            } else if (timeType.equals("year")) {
                calendar.setTime(end);
                calendar.add(Calendar.YEAR,-1);
                start=calendar.getTime();
            }
            map.put("startTime", sdf.format(start));
            map.put("endTime", sdf.format(end));
        }else{
            map.put("startTime", startTime);
            map.put("endTime", endTime);
        }
        int total=0;
        if(rankType!=null&&rankType!=""){
            if("1".equals(rankType)){
                map.put("gridId",gridId);
                map1.put("gridId",gridId);
                List<StatisticsModel> rankListDetailByGrid = gridEventMapper.getRankListDetailByGrid(map);
                if(rankListDetailByGrid!=null){
                    baseWrapper.setData(rankListDetailByGrid);
                    baseWrapper.setTotle(total);

                    baseWrapper.setFlag(true);
                    baseWrapper.setMsg("ok");
                }
                return  baseWrapper;
            }
            if("2".equals(rankType)){
                map.put("userId",userId);
                map1.put("userId",userId);
                eventRankListByGrid = gridEventMapper.getRankListDetailByUser(map);
                total=gridEventMapper.getRankListDetailByUserNum(map1);
                if(eventRankListByGrid!=null){
                    for(RankListDetailModel gridEvent:eventRankListByGrid){
                        if(gridEvent.getRumId()!=0) {
                            FlowRunPrcs flowRunPrcs = flowRunPrcsMapper.queryByRunId(gridEvent.getRumId());
                            if (flowRunPrcs != null) {
                                gridEvent.setPrcsId(flowRunPrcs.getPrcsId().toString());
                            } else {
                                gridEvent.setPrcsId("");
                            }
                        }
                    }

                }
            }
            if(eventRankListByGrid!=null){
                baseWrapper.setData(eventRankListByGrid);
                baseWrapper.setTotle(total);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("ok");
            }else{
                baseWrapper.setData(eventRankListByGrid);
                baseWrapper.setTotle(total);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("数据为空");
            }
        }
        return baseWrapper;
    }
    /**
    * @Author 程叶同
    * @Description 事件统计三级穿透
    * @Date 11:14 2018/1/27
    */
    public BaseWrapper getThreeDetail(Integer page, Integer pageSize, boolean useFlag, String timeType, String gridId, String userId, String startTime, String endTime){
        BaseWrapper baseWrapper=new BaseWrapper();
        List<RankListDetailModel> eventRankListByGrid=null;
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("pageParams",pageParams);
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(timeType!=null&&timeType!=""){
            Date start=null;
            Date end=new Date();
            if (timeType.equals("month")) {
                calendar.setTime(end);
                calendar.add(Calendar.MONTH,-1);
                start=calendar.getTime();

            } else if (timeType.equals("threeMonth")) {
                calendar.setTime(end);
                calendar.add(Calendar.MONTH,-3);
                start=calendar.getTime();
            } else if (timeType.equals("halfYear")) {
                calendar.setTime(end);
                calendar.add(Calendar.MONTH,-6);
                start=calendar.getTime();

            } else if (timeType.equals("year")) {
                calendar.setTime(end);
                calendar.add(Calendar.YEAR,-1);
                start=calendar.getTime();
            }
            map.put("startTime", sdf.format(start));
            map.put("endTime", sdf.format(end));
            map1.put("startTime", sdf.format(start));
            map1.put("endTime", sdf.format(end));
        }else{
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map1.put("startTime", startTime);
            map1.put("endTime", endTime);
        }
        map.put("gridId",gridId);
        map.put("userId",userId);
        map1.put("gridId",gridId);
        map1.put("userId",userId);
        int total=0;
        eventRankListByGrid = gridEventMapper.getThreeDetail(map);
        total=gridEventMapper.getThreeDetailNum(map1);

        if(eventRankListByGrid!=null){
            for(RankListDetailModel gridEvent:eventRankListByGrid){
                if(gridEvent.getRumId()!=0) {
                    FlowRunPrcs flowRunPrcs = flowRunPrcsMapper.queryByRunId(gridEvent.getRumId());
                    if (flowRunPrcs != null) {
                        gridEvent.setPrcsId(flowRunPrcs.getPrcsId().toString());
                    } else {
                        gridEvent.setPrcsId("");
                    }
                }
            }
            baseWrapper.setData(eventRankListByGrid);
            baseWrapper.setTotle(total);
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("ok");
        }else{
            baseWrapper.setData(eventRankListByGrid);
            baseWrapper.setTotle(total);
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("数据为空");
        }
        return  baseWrapper;
    }


    public ToJson tinCount(String timeType){
        Map<String,Object> map1=new HashMap<String,Object>();
        map1.put("timeType",timeType);
        map1.put("parting","1");
        map1.put("prcsFlag","1");

        Map<String,Object> map2=new HashMap<String,Object>();
        map2.put("timeType",timeType);
        map2.put("parting","1");
        map2.put("prcsFlag","2");
            ToJson json=new ToJson();
            try{
                int counjinxz=gridEventMapper.selSendOrReceivetuisCopyOne(map1);//进行中
                int ton=gridEventMapper.selSendOrReceivetuisCopyOne(map2);//已完成
                int mnt=counjinxz+ton;
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("counjinxz",counjinxz);
                map.put("cston",ton);
                map.put("mnt",mnt);
                json.setObject(map);
                json.setFlag(0);
            }catch (Exception e){
                e.printStackTrace();
            }
            return  json;
    }


    /**
     * 辦理中 已完成
     * @param request
     * @param page
     * @param pageSize
     * @param prcsFlag
     * @return
     */
    public BaseWrappers selSendOrReceivetuis(HttpServletRequest request, Integer page, Integer pageSize, Integer prcsFlag, Integer parting) {
        BaseWrappers wrappers = new BaseWrappers();
        wrappers.setStatus(true);
        wrappers.setFlag(false);

        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        if (StringUtils.checkNull(users.getUserId())) {
            wrappers.setMsg("用户信息过期");
            return wrappers;
        }


        page = (page - 1) * pageSize;
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("page", page);
        param.put("pageSize", pageSize);
        param.put("prcsFlag", prcsFlag);
        param.put("parting", parting);
        param.put("user", users.getUid());
        List<GridEventFlowRunPrcs>  datas=gridEventMapper.selSendOrReceivetuis(param);
        if(datas!=null&&datas.size()>0){
            for(GridEventFlowRunPrcs gridEvent:datas){
                // 超时计算方法  0-本步骤接收后开始计时,1-上一步骤转交后开始计时
                if (gridEvent.getTimeOutType().equals("0")) {
                    // 判断该步骤状态  1-未接收,2-办理中,3-转交下一步，下一步经办人无人接收,4-已办结,5-自由流程预设步骤,6-已挂起
                    if (gridEvent.getPrFlag().equals(2)) {
                        if (!StringUtils.checkNull(gridEvent.getPrcsTime())) {
                            setTimeOutStr(gridEvent, DateFormat.getTime(gridEvent.getPrcsTime()));
                        }
                    } else if (gridEvent.getPrFlag().equals(1)) {
                        gridEvent.setIfOutTime(true);
                        gridEvent.setTimeOutStr("计时未开始");
                    }
                } else if (gridEvent.getTimeOutType().equals("1")) {
                    if (!gridEvent.getRealPrcsId().equals(1)) {
                        if (!gridEvent.getPrFlag().equals(3) && !gridEvent.getPrFlag().equals(4)) {
                            if (!StringUtils.checkNull(gridEvent.getCreateTime())) {
                                setTimeOutStr(gridEvent, DateFormat.getTime(gridEvent.getCreateTime()));
                            }
                        } else if (gridEvent.getPrFlag().equals(3)) {
                            gridEvent.setTimeOutStr("计时已结束");
                            gridEvent.setIfOutTime(true);
                        } else if (gridEvent.getPrFlag().equals(4)) {
                            gridEvent.setTimeOutStr("计时已结束");
                            gridEvent.setIfOutTime(true);
                        }
                    }
                }
                SysCode singleCode = sysCodeMapper.getSingleCode(gridEvent.getEventTypeParent(), gridEvent.getEventType());
                if(singleCode!=null){
                    gridEvent.setCodeName(singleCode.getCodeName());
                }else{
                    gridEvent.setCodeName("");
                }

                gridEvent.setDateTime(DateFormat.getStrDate(gridEvent.getEventTime()));
                if(gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId())!=null){
                    gridEvent.setGridName(gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId()).getGridName());
                }
            }
        }

        Integer total=gridEventMapper.selSendOrReceivetuisCounts(param);
        wrappers.setTotal(total);
        wrappers.setFlag(true);
        wrappers.setDatas(datas);
        return wrappers;
    }


    /**
     * 辦理中 已完成
     * @param request
     * @param page
     * @param pageSize
     * @param prcsFlag
     * @return
     */
    public BaseWrappers selSendOrReceivetuis(HttpServletRequest request, Integer page, Integer pageSize, Integer prcsFlag, Integer parting, Integer shouji, String timeType) {
        BaseWrappers wrappers = new BaseWrappers();
        wrappers.setStatus(true);
        wrappers.setFlag(false);

        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        if (StringUtils.checkNull(users.getUserId())) {
            wrappers.setMsg("用户信息过期");
            return wrappers;
        }


        page = (page - 1) * pageSize;
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("page", page);
        param.put("pageSize", pageSize);
        param.put("prcsFlag", prcsFlag);
        param.put("parting", parting);
        param.put("timeType",timeType);
   /*     if(users.getDeptId()!=1 && shouji!=1){
            param.put("user", users.getUid());
        }*/
        List<GridEventFlowRunPrcs>  datas=gridEventMapper.selSendOrReceivetuis(param);
        if(datas!=null&&datas.size()>0){
            for(GridEventFlowRunPrcs gridEvent:datas){
                // 超时计算方法  0-本步骤接收后开始计时,1-上一步骤转交后开始计时
                if (gridEvent.getTimeOutType().equals("0")) {
                    // 判断该步骤状态  1-未接收,2-办理中,3-转交下一步，下一步经办人无人接收,4-已办结,5-自由流程预设步骤,6-已挂起
                    if (gridEvent.getPrFlag().equals(2)) {
                        if (!StringUtils.checkNull(gridEvent.getPrcsTime())) {
                            setTimeOutStr(gridEvent, DateFormat.getTime(gridEvent.getPrcsTime()));
                        }
                    } else if (gridEvent.getPrFlag().equals(1)) {
                        gridEvent.setIfOutTime(true);
                        gridEvent.setTimeOutStr("计时未开始");
                    }
                } else if (gridEvent.getTimeOutType().equals("1")) {
                    if (!gridEvent.getRealPrcsId().equals(1)) {
                        if (!gridEvent.getPrFlag().equals(3) && !gridEvent.getPrFlag().equals(4)) {
                            if (!StringUtils.checkNull(gridEvent.getCreateTime())) {
                                setTimeOutStr(gridEvent, DateFormat.getTime(gridEvent.getCreateTime()));
                            }
                        } else if (gridEvent.getPrFlag().equals(3)) {
                            gridEvent.setTimeOutStr("计时已结束");
                            gridEvent.setIfOutTime(true);
                        } else if (gridEvent.getPrFlag().equals(4)) {
                            gridEvent.setTimeOutStr("计时已结束");
                            gridEvent.setIfOutTime(true);
                        }
                    }
                }
                SysCode singleCode = sysCodeMapper.getSingleCode(gridEvent.getEventTypeParent(), gridEvent.getEventType());
                if(singleCode!=null){
                    gridEvent.setCodeName(singleCode.getCodeName());
                }else{
                    gridEvent.setCodeName("");
                }

                gridEvent.setDateTime(DateFormat.getStrDate(gridEvent.getEventTime()));
                if(gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId())!=null){
                    gridEvent.setGridName(gridInfoMapper.selectByPrimaryKey(gridEvent.getGridId()).getGridName());
                }
            }
        }

        Integer total=gridEventMapper.selSendOrReceivetuisCounts(param);
        wrappers.setTotal(total);
        wrappers.setFlag(true);
        wrappers.setDatas(datas);
        return wrappers;
    }
    public BaseWrappers getByTypeByAppearance(HttpServletRequest request){
        BaseWrappers baseWrappers=new BaseWrappers();
        Map<String, Object> map = new HashMap<String, Object>();
        List<StatisticsModel> statisticsModels = gridEventMapper.getByTypeByAppearance(map);
        baseWrappers.setTotal(statisticsModels.get(0).getTotal());
        baseWrappers.setDatas(statisticsModels);
        baseWrappers.setFlag(true);
        baseWrappers.setStatus(true);
        return  baseWrappers;

    }

    public BaseWrappers gridEventTimeByGroup(HttpServletRequest request) throws ParseException {
        BaseWrappers baseWrappers = new BaseWrappers();
        //得到Calendar的实例
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Calendar calendar1 = Calendar.getInstance();
        String date1 = dateFormat.format(calendar.getTime());
        calendar1.add(Calendar.YEAR, -1);
        String date2 = dateFormat.format(calendar1.getTime());
        List<String> dates = DateCompute.getMonthBetween(date2, date1);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", date2);
        map.put("end", date1);
        Map<String, String> dateMap = new HashMap<>();
        Map<String, String> dateMa1 = new HashMap<>();
        for (String str : dates) {
            dateMap.put(str, "0");
            dateMa1.put(str, "0");
        }
        StatisticsModel weichuliModel=new StatisticsModel();
        StatisticsModel yichuliModel=new StatisticsModel();

        List<StatisticsModel> statisticsModels = gridEventMapper.gridEventTimeByGroup(map);
        if(statisticsModels!=null&&statisticsModels.size()>0){
            for (StatisticsModel statisticsModel : statisticsModels) {
                for (Map.Entry<String, String> entry : dateMap.entrySet()) {
                    if (statisticsModel.getName().equals(entry.getKey())) {
                        dateMap.put(statisticsModel.getName(), String.valueOf(statisticsModel.getY()));
                    }

                }

            }
        }
        weichuliModel.setName("待处理");
        weichuliModel.setDateMap(sortMapByKey(dateMap));
        List<StatisticsModel> statisticsModels1 = gridEventMapper.gridEventTimeByGroup1(map);
        if(statisticsModels1!=null&&statisticsModels1.size()>0){
            for (StatisticsModel statisticsModel1 : statisticsModels1) {
                for (Map.Entry<String, String> entry : dateMap.entrySet()) {
                    if (statisticsModel1.getName().equals(entry.getKey())) {
                        dateMa1.put(statisticsModel1.getName(), String.valueOf(statisticsModel1.getY()));
                    }

                }

            }
        }
        yichuliModel.setName("已完成");
        yichuliModel.setDateMap(sortMapByKey(dateMa1));
        List<StatisticsModel> statisticsModels2=new ArrayList<>();
        statisticsModels2.add(weichuliModel);
        statisticsModels2.add(yichuliModel);
        baseWrappers.setDatas(statisticsModels2);
        baseWrappers.setFlag(true);
        baseWrappers.setStatus(true);
        return baseWrappers;
    }

    /**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, String> sortMap = new TreeMap<String, String>(
                new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }
    static class MapKeyComparator implements Comparator<String>{

        @Override
        public int compare(String str1, String str2) {

            return str1.compareTo(str2);
        }
    }
    public BaseWrappers gridEventByRumId(int rumId){
        BaseWrappers baseWrappers=new BaseWrappers();
        try{
            GridEvent gridEvent=gridEventMapper.gridEventByRumId(rumId);
            baseWrappers.setMsg("ok");
            baseWrappers.setObject(gridEvent);
            baseWrappers.setFlag(true);
        }catch (Exception e){
            baseWrappers.setMsg("err");
            baseWrappers.setFlag(false);
        }

        return baseWrappers;
    }
}

